package com.work.washer.controller;

import com.work.washer.dto.MachineReservationReq;
import com.work.washer.dto.MachineVo;
import com.work.washer.model.Comment;
import com.work.washer.model.Machine;
import com.work.washer.model.Type;
import com.work.washer.model.UseRecord;
import com.work.washer.service.MachineService;
import com.work.washer.service.TypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Tag(name = "洗护")
@RestController
@RequestMapping("/wash")
public class WashController {

    @Autowired
    private MachineService machineService;


    @Autowired
    private TypeService typeService;

    /**
     * 添加
     *
     * @param book
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @Operation(summary = " 新增")
    @PostMapping("/save")
    public boolean save(@RequestBody Machine machine) {
        return machineService.save(machine);
    }


    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @Operation(summary = " 删除")
    @GetMapping("/remove/{id}")
    public boolean remove(@PathVariable Integer id) {
        return machineService.removeById(id);
    }


    /**
     * 根据主键更新
     *
     * @param book
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @Operation(summary = " 更新")
    @PostMapping("/update")
    public boolean update(@RequestBody Machine machine) {
        return machineService.updateById(machine);
    }


    /**
     * 查询所有
     *
     * @return 所有数据
     */
    @Operation(summary = "设备列表")
    @GetMapping("/list")
    public List<MachineVo> list() {
        machineService.updateStatus();
        List<Type> list = typeService.list();
        List<MachineVo> machineVos = new ArrayList<>();
        list.forEach(e -> {
            MachineVo machineVo = new MachineVo();
            machineVo.setTypeName(e.getName());
            machineVo.setTypeId(e.getId());
            List<Machine> list1 = machineService.lambdaQuery().eq(Machine::getType, e.getId()).list();
            machineVo.setMachines(list1);
            machineVos.add(machineVo);
        });
        return machineVos;
    }


    /**
     * 根据主键获取详细信息。
     *
     * @param id type主键
     * @return 详情
     */
    @Operation(summary = "设备详情")
    @GetMapping("/getInfo/{id}")
    public Machine getInfo(@PathVariable Integer id) {
        return machineService.getById(id);
    }

    @Operation(summary = "预约")
    @PostMapping("/reservation")
    public boolean reservation(@RequestBody @Valid MachineReservationReq req) throws ParseException {
        return machineService.reservation(req);
    }


    @Operation(summary = "直接使用")
    @PostMapping("/use")
    public boolean useBook(@RequestBody MachineReservationReq reservationReq) {
        return machineService.useBook(reservationReq);
    }

    @Operation(summary = "从预约记录使用")
    @GetMapping("/useRecord")
    public boolean useRecord(@RequestParam("recordId") @Schema(title = "预约Id") Integer recordId,
                             @RequestParam("userId") @Schema(title = "用户id") Integer userId) {
        return machineService.useRecord(recordId, userId);
    }

    @Operation(summary = "使用结束")
    @GetMapping("/end")
    public boolean end(@RequestParam("recordId") @Schema(title = "预约Id") Integer recordId,
                       @RequestParam("userId") @Schema(title = "用户id") Integer userId) {
        return machineService.end(recordId, userId);
    }

    @Operation(summary = "查询个人的预约信息")
    @GetMapping("/listRecord")
    public List<UseRecord> listRecord(Integer userId,
                                      @Schema(title = "状态：1我的预约、2我的使用") Integer status) {
        return machineService.listRecord(userId, status);
    }

    @Operation(summary = "新增评论")
    @GetMapping("/addComment")
    public boolean addComment(@RequestParam("machineId") @Schema(title = "设备Id") Integer machineId,
                              @RequestParam("userId") @Schema(title = "用户id") Integer userId,
                              @RequestParam("content") @Schema(title = "内容") String content) {
        return machineService.addComment(machineId, userId, content);
    }

    @Operation(summary = "获取评论")
    @GetMapping("/getComment")
    public List<Comment> getComment(@RequestParam("machineId") @Schema(title = "设备Id") Integer machineId) {
        return machineService.getComment(machineId);
    }

    @Operation(summary = "投诉退费")
    @GetMapping("/refunds")
    public Boolean refunds(@RequestParam("recordId") @Schema(title = "记录id") Integer recordId){
        return machineService.refunds(recordId);
    }

    @Operation(summary = "设备使用记录")
    @GetMapping("/record")
    public List<UseRecord> record(@RequestParam("machineId") @Schema(title = "设备id") Integer machineId){
        return machineService.record(machineId);
    }



}